var t = require("../@babel/runtime/helpers/interopRequireDefault"), e = t(require("../@babel/runtime/regenerator")), i = require("../@babel/runtime/helpers/asyncToGenerator"), a = require("../@babel/runtime/helpers/createForOfIteratorHelper"), n = t(require("../utils/weapp.qrcode.esm.js"));

module.exports = Behavior({
    behaviors: [],
    properties: {
        isshow: {
            value: !1,
            type: Boolean
        },
        poster_id: {
            type: null
        },
        showcarrfootBol: {
            type: null
        },
        iPnoneBottomBol: {
            type: null
        },
        plugin_name: {
            type: null,
            value: ""
        },
        posterData: {
            type: Object,
            value: function() {
                return {};
            }
        },
        defaultImg: {
            type: String,
            value: null
        }
    },
    data: {
        errorFlag: !1,
        errorMsg: "",
        poster: "",
        loading: 0,
        getFlag: !0,
        step: 1,
        skipError: !1,
        unsafeList: [],
        image_security_verification: !1
    },
    observers: {
        isshow: function(t) {
            "posterCenter" == this.data.plugin_name ? t && this.initPosterData() : t && "" == this.data.poster && this.data.getFlag && (this.getPosterPic(), 
            this.data.getFlag = !1);
        }
    },
    attached: function() {},
    methods: {
        loadImgError: function(t) {
            this.setData({
                errorFlag: !0,
                errorMsg: t.detail.errMsg
            });
        },
        clicktapshow: function() {
            this.setData({
                isshow: !1
            }), this.triggerEvent("closeMethod", {
                isshow: this.data.isshow
            });
        },
        previewImage: function() {
            wx.showToast({
                title: "长按图片分享好友",
                icon: "none",
                duration: 1800
            });
        },
        drawCanvasImage: function(t, e) {
            var i = this.data.step;
            t.drawImage(e.img, e.left * i, e.top * i, e.width * i, e.height * i);
        },
        drawCanvasCircular: function(t, e) {
            var i = this.data.step, a = e.left * i, n = e.top * i, r = e.width * i, o = e.height * i;
            t.save(), t.beginPath(), t.arc(r / 2 + a, o / 2 + n, r / 2, 0, 2 * Math.PI, !1), 
            t.clip(), t.drawImage(e.img, a, n, r, o), t.restore();
        },
        drawCanvasFillRect: function(t, e) {
            var i = this.data.step, a = e.left * i, n = e.top * i, r = e.width * i, o = e.height * i;
            t.setFillStyle(e.color), t.fillRect(a, n, r, o);
        },
        drawCanvasText: function(t, e) {
            var i = this.data.step, a = e.size * i, n = (e.top + e.size) * i, r = e.left * i;
            t.setFillStyle(e.color);
            var o = "normal", s = "normal";
            e.bold && 1 == e.bold && (o = "bold"), e.italic && 1 == e.italic && (s = "italic"), 
            t.font = s + " small-caps " + o + " 30px arial,sans-serif", t.setFontSize(a), e.slide && 1 == e.slide ? this.drawCanvasFillRect(t, {
                left: e.left,
                top: e.top + e.size / 2,
                width: t.measureText(e.src).width / i,
                height: 3,
                color: e.color
            }) : e.under && 1 == e.under && this.drawCanvasFillRect(t, {
                left: e.left,
                top: e.top + e.size,
                width: t.measureText(e.src).width / i,
                height: 3,
                color: e.color
            }), t.fillText(e.src, r, n);
        },
        drawCanvasLineText: function(t, e) {
            var i = this.data.step, a = e.size * i, n = (e.top + e.size) * i, r = e.src.split(""), o = "", s = [];
            t.setFontSize(a), t.setFillStyle(e.color);
            for (var h = 0; h < r.length; h++) t.measureText(o).width < e.width * i ? o += r[h] : (h--, 
            s.push(o), o = "");
            if (s.push(o), s.length > e.line) {
                for (var l = s.slice(0, s.length), c = l[e.line - 1], u = "", d = [], f = 0; f < c.length && t.measureText(u).width < e.width * i; f++) u += c[f];
                d.push(u);
                var g = d[0] + "...";
                l.splice(e.line - 1, 1, g), s = l;
            }
            for (var w = 0; w < s.length; w++) if (w < e.line) {
                var p = "normal", m = "normal";
                e.bold && 1 == e.bold && (p = "bold"), e.italic && 1 == e.italic && (m = "italic"), 
                t.font = m + " small-caps " + p + " 30px arial,sans-serif", t.setFontSize(a), e.slide && 1 == e.slide ? this.drawCanvasFillRect(t, {
                    left: e.left,
                    top: e.top + e.size / 2 + (w * e.size + 10 * w / i),
                    width: t.measureText(s[w]).width / i,
                    height: 3,
                    color: e.color
                }) : e.under && 1 == e.under && this.drawCanvasFillRect(t, {
                    left: e.left,
                    top: e.top + e.size + (w * e.size + 10 * w / i),
                    width: t.measureText(s[w]).width / i,
                    height: 3,
                    color: e.color
                }), t.fillText(s[w], e.left * i, n + (w * a + 10 * w));
            }
        },
        measureTextWidth: function(t, e) {
            var i = e.width, a = e.text, n = e.size, r = this.data.step, o = a.split(""), s = "", h = [];
            t.setFontSize(n * r);
            for (var l = 0; l < o.length; l++) t.measureText(s).width < i * r ? s += o[l] : (l--, 
            h.push(s), s = "");
            h.push(s);
            var c = t.measureText(s).width, u = s;
            return h.length > 1 && (c = t.measureText(h[0] + "...").width, u = h[0] + "..."), 
            {
                actualWidth: c / r,
                actualText: u
            };
        },
        toimage: function(t, e) {
            var i = this;
            return new Promise(function(a, n) {
                wx.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: e.width,
                    height: e.height,
                    destWidth: 4 * e.width,
                    destHeight: 4 * e.height,
                    canvasId: t,
                    success: function(t) {
                        var e = t.tempFilePath;
                        a(e);
                    },
                    fail: function(t) {
                        wx.showToast({
                            title: "图片导出失败" + t,
                            icon: "success",
                            duration: 2e3
                        });
                    }
                }, i);
            });
        },
        drawRoundRectPath: function(t, e, i, a) {
            t.beginPath(0), t.arc(e - a, i - a, a, 0, Math.PI / 2), t.lineTo(a, i), t.arc(a, i - a, a, Math.PI / 2, Math.PI), 
            t.lineTo(0, a), t.arc(a, a, a, Math.PI, 3 * Math.PI / 2), t.lineTo(e - a, 0), t.arc(e - a, a, a, 3 * Math.PI / 2, 2 * Math.PI), 
            t.lineTo(e, i - a), t.closePath(), t.clip();
        },
        downImage: function(t) {
            return this.data.image_security_verification ? this.readDownPicInfo(t.src) : this.unsafeReadDownPicInfo(t);
        },
        readDownPicInfo: function(t) {
            var e = this;
            return new Promise(function(i, a) {
                wx.downloadFile({
                    url: t,
                    success: function(n) {
                        wx.getImageInfo({
                            src: n.tempFilePath,
                            success: function(t) {
                                i({
                                    img: n.tempFilePath,
                                    width: t.width,
                                    height: t.height
                                });
                            },
                            fail: function(i) {
                                e.data.getFlag = !0;
                                e.setData({
                                    errorFlag: !0,
                                    errorMsg: "图片读取出错,错误原因:" + i.errMsg + ",图片地址:" + t + ",微信临时文件地址格式：" + n.tempFilePath + ",确认图片地址是否为404，微信临时地址格式是不是png，不是的话，在服务端响应的 header 中指定合理的 Content-Type 字段，以保证客户端正确处理文件类型。"
                                }), a({
                                    error: "错误"
                                });
                            }
                        });
                    },
                    fail: function(i) {
                        e.data.getFlag = !0;
                        var a = "未知原因,自行检查图片地址是否存在";
                        "downloadFile:fail createDownloadTask:fail url not in domain list" != i.errMsg && "downloadFile:fail url not in domain list" != i.errMsg || (a = "请查看是否设置downloadFile安全域名"), 
                        e.setData({
                            errorFlag: !0,
                            errorMsg: "图片下载出错,错误原因:" + i.errMsg + ",图片地址:" + t + "," + a
                        });
                    }
                });
            });
        },
        unsafeReadDownPicInfo: function(t) {
            var e = this;
            return new Promise(function(i, a) {
                e.createSelectorQuery().select("#temp_security_id").fields({
                    node: !0,
                    size: !0
                }).exec(function(a) {
                    var n = a[0].node, r = n.getContext("2d"), o = n.createImage();
                    o.src = t.src, o.onload = function(a) {
                        var s = t.width ? t.width : o.width, h = t.height ? t.height : o.height;
                        n.width = s, n.height = h, r.drawImage(o, 0, 0, s, h);
                        var l = n.toDataURL("image/png"), c = wx.env.USER_DATA_PATH + "/poster_" + e.randomWord(5, 10) + ".png", u = l.replace(/^data:image\/\w+;base64,/, "");
                        wx.getFileSystemManager().writeFileSync(c, u, "base64"), e.data.unsafeList.push(c), 
                        i({
                            img: c,
                            width: s,
                            height: h
                        });
                    }, o.onerror = function(i) {
                        e.setData({
                            errorFlag: !0,
                            errorMsg: "图片地址:" + t.src + ",图片加载出错，检查图片地址是否存在，能不能在浏览器正常打开，能的话在服务端响应的 header 中指定合理的 Content-Type 字段，以保证客户端正确处理文件类型。"
                        });
                    };
                });
            });
        },
        removeUnsafeList: function() {
            var t, e = wx.getFileSystemManager(), i = a(this.data.unsafeList);
            try {
                for (i.s(); !(t = i.n()).done; ) {
                    var n = t.value;
                    e.unlinkSync(n);
                }
            } catch (t) {
                i.e(t);
            } finally {
                i.f();
            }
            this.data.unsafeList = [];
        },
        randomWord: function(t, e) {
            var i = new Date().valueOf(), a = t, n = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ];
            2 == arguments.length && (a = Math.round(Math.random() * (e - t)) + t);
            for (var r = 0; r < a; r++) {
                var o = Math.round(Math.random() * (n.length - 1));
                i += n[o];
            }
            return i;
        },
        getMyCanvasMessage: function(t, e) {
            var i = this;
            return new Promise(function(a, n) {
                wx.getSystemInfo({
                    success: function(n) {
                        n.screenWidth, n.screenWidth;
                        i.data.step, i.data.step, a({
                            width: t * i.data.step,
                            height: e * i.data.step,
                            contentScale: 1
                        });
                    },
                    fail: function(t) {
                        i.data.getFlag = !0;
                    }
                });
            });
        },
        getQRCodeUrl: function(t) {
            var a = this;
            return new Promise(function(r, o) {
                var s = a.data.step;
                a.setData({
                    tempQRCodeCanvasWidth: t.width * s,
                    tempQRCodeCanvasHeight: t.height * s
                }, i(e.default.mark(function o() {
                    return e.default.wrap(function(o) {
                        for (;;) switch (o.prev = o.next) {
                          case 0:
                            (0, n.default)({
                                width: (t.width - 10) * s,
                                height: (t.height - 10) * s,
                                canvasId: "tempQRCode",
                                background: "#fff",
                                _this: a,
                                x: 5 * s,
                                y: 5 * s,
                                text: t.src,
                                callback: function() {
                                    setTimeout(i(e.default.mark(function i() {
                                        var n;
                                        return e.default.wrap(function(e) {
                                            for (;;) switch (e.prev = e.next) {
                                              case 0:
                                                return e.next = 2, a.toimage("tempQRCode", {
                                                    width: t.width * s,
                                                    height: t.height * s
                                                });

                                              case 2:
                                                n = e.sent, r(n);

                                              case 4:
                                              case "end":
                                                return e.stop();
                                            }
                                        }, i);
                                    })), 500);
                                }
                            });

                          case 1:
                          case "end":
                            return o.stop();
                        }
                    }, o);
                })));
            });
        },
        setProportion: function(t, e) {
            if (e.height > e.width) {
                var i = t.height / e.height;
                e.height = e.height * i, e.width = e.width * i;
            } else {
                var a = t.width / e.width;
                e.width = e.width * a, e.height = e.height * a;
            }
            e.width > t.width && (e.height = e.height / (e.width / t.width), e.width = t.width), 
            e.height > t.height && (e.width = e.width / (e.height / t.height), e.height = t.height);
        },
        pxToNum: function(t) {
            var e = t.substring(0, t.length - 2);
            return parseInt(e);
        },
        checkWritePotosAlbum: function() {
            var t = this;
            return i(e.default.mark(function i() {
                var a;
                return e.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return a = t, e.next = 3, t.getAuthSetting("scope.writePhotosAlbum");

                      case 3:
                        if (e.sent) {
                            e.next = 7;
                            break;
                        }
                        return wx.showModal({
                            title: "请开启授权",
                            content: "是否跳转设置页开启授权",
                            success: function(t) {
                                t.confirm ? wx.openSetting({
                                    success: function(t) {
                                        t.authSetting["scope.writePhotosAlbum"] ? a.saveImg() : console.log("还是没授权");
                                    }
                                }) : t.cancel && console.log("用户点击取消");
                            }
                        }), e.abrupt("return");

                      case 7:
                        t.saveImg();

                      case 8:
                      case "end":
                        return e.stop();
                    }
                }, i);
            }))();
        },
        saveImg: function() {
            var t = this.data.poster;
            wx.downloadFile({
                url: t,
                success: function(t) {
                    wx.saveImageToPhotosAlbum({
                        filePath: t.tempFilePath,
                        success: function(t) {
                            wx.showToast({
                                title: "保存成功",
                                icon: "none",
                                duration: 2e3
                            });
                        },
                        fail: function(t) {
                            console.log(t);
                        }
                    });
                },
                fail: function(e) {
                    wx.saveImageToPhotosAlbum({
                        filePath: t,
                        success: function(t) {
                            wx.showToast({
                                title: "保存成功",
                                icon: "none",
                                duration: 2e3
                            });
                        },
                        fail: function(t) {
                            console.log(t);
                        }
                    });
                }
            });
        },
        getAuthSetting: function(t) {
            return new Promise(function(e, i) {
                wx.getSetting({
                    success: function(i) {
                        i.authSetting[t] ? e(!0) : wx.authorize({
                            scope: t,
                            success: function() {
                                e(!0);
                            },
                            fail: function() {
                                e(!1);
                            }
                        });
                    },
                    fail: function() {
                        e(!1);
                    }
                });
            });
        }
    }
});